﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Core.DataAccess.IRepositories;

namespace Core.DataAccess.Repositories
{
    [Export(typeof(IMessageRepository))]
    public class MessageRepository : IMessageRepository
    {
        private Connection conn;

        public MessageRepository()
        {
            conn = new Connection();
        }

        public int CountMessages(int accountID)
        {
            using (MXHEntities context = conn.GetContext())
            {
                return context.Messages.Where(n => n.AccountID == accountID && n.IsRead == false).Select(n => n.AccountID).Count();
            }
        }

        public void SaveMessage(Message mesage)
        {
            using (MXHEntities context = conn.GetContext())
            {
                context.Messages.AddObject(mesage);
                context.SaveChanges();
            }
        }

        public List<Message> GetMessagesByAccountID(int accountID)
        {
            List<Message> lstMessages = new List<Message>();

            using (MXHEntities context = conn.GetContext())
            {
                var messages = (from msg in context.Messages
                                where msg.AccountID == accountID
                                select msg).Take(10).ToList();
                if (messages != null)
                    lstMessages = messages.ToList();
            }
            return lstMessages;
        }

        public void UpdateMessageIsRead(Message msg)
        {
            Message message = null;
            using (MXHEntities context = conn.GetContext())
            {
                message = (from mes in context.Messages
                          where mes.AccountID == msg.AccountID && mes.FriendAccountID == msg.FriendAccountID
                          select mes).FirstOrDefault();
                if (message != null)
                {
                    message.IsRead = true;
                    context.SaveChanges();
                }
            }         
        }

        public void DeleteMessageByAccountID(int accountID, int friendID)
        {
            using (MXHEntities context = conn.GetContext())
            {
                var item = from mes in context.Messages
                           where (mes.AccountID == accountID && mes.FriendAccountID == friendID) ||
                                 (mes.FriendAccountID == accountID && mes.AccountID == friendID)
                           select mes;
                if (item.Count() > 0)
                {
                    List<Message> lstMess = item.ToList();
                    foreach (Message msg in lstMess)
                    {
                        context.Messages.DeleteObject(msg);
                        context.SaveChanges();
                    }
                }
            }        
        }
    }
}
